package com.lagou.dao.Impl;

import com.lagou.dao.CourseDao;
import com.lagou.pojo.Course;
import com.lagou.utils.DruidUtils;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanListHandler;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class CourseDaoImpl implements CourseDao {


    @Override
    public List<Course> findCourseNameAndStatus(String course_name, String status) {
       try{
        QueryRunner qr = new QueryRunner(DruidUtils.getDataSource());

        //sql语句查询
        String sql="select id,course_name,price,sort_num,STATUS from course where is_del=?";
        //StringBuffer缓存区 append()
        StringBuffer sb  = new StringBuffer("select id,course_name,price,sort_num,STATUS from course where is_del=?");

        List<Object> List =new ArrayList<>();
        List.add(0);

          if(course_name!=null){
              course_name ="%"+course_name+"%";
              sb.append(" and course_name like ?");

              List.add(course_name);

          }else if (status!=null){
              sb.append(" and status = ?");

              //string 转int
              int i =Integer.parseInt(status);
              List.add(i);


          }


        List<Course>  result = qr.query(sb.toString(),new BeanListHandler<Course>(Course.class),List.toArray());
          return result;
        } catch (SQLException e) {
            e.printStackTrace();
        }

        return null;

    }

    @Override
    public List<Course> findCourseList() {
        try {
        QueryRunner qr = new QueryRunner(DruidUtils.getDataSource());
         //sql
            String sql ="select id,course_name,price,sort_num,STATUS from course where is_del=?";
        List<Course> query =qr.query(sql, new BeanListHandler<Course>(Course.class), 0);
        return query;
        } catch (SQLException e) {
            e.printStackTrace();
            return null;

        }



    }

    @Override
    public int  SaveCourseSales(Course course) {
        try{
        QueryRunner qr = new QueryRunner(DruidUtils.getDataSource());
        String sql ="INSERT INTO course(\n" +
                "course_name,\n" +
                "brief,\n" +
                "teacher_name,\n" +
                "teacher_info,\n" +
                "preview_first_field,\n" +
                "preview_second_field,\n" +
                "discounts,\n" +
                "price,\n" +
                "price_tag,\n" +
                "share_image_title,\n" +
                "share_title,\n" +
                "share_description,\n" +
                "course_description,\n" +
                "course_img_url,\n" +
                "STATUS,\n" +
                "create_time,\n" +
                "update_time\n" +
                ")VALUES(?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?);";
        Object[] param = {
                course.getCourse_name(),course.getBrief(),course.getTeacher_name(),course.getTeacher_info(),
                course.getPreview_first_field(),course.getPreview_second_field(),course.getDiscounts(),course.getPrice(),
                course.getPrice_tag(),course.getShare_image_title(),course.getShare_title(),course.getShare_description(),
                course.getCourse_description(),course.getCourse_img_url(),course.getStatus(),course.getCreate_time(),course.getUpdate_time()
        };

        //update(sql params) params 一个一个的[1、2、3]
            int row = qr.update(sql, param);
            return row;
        } catch (SQLException e) {
            e.printStackTrace();
        }

        return 0;
    }
    //获取ID ctrl+i alt+insert

    @Override
    public List<Course> findCourseById(int id) {
        try{
        QueryRunner qr = new QueryRunner(DruidUtils.getDataSource());
        String sql = "SELECT \n" +
                "id,\n" +
                "course_name,\n" +
                "brief,\n" +
                "teacher_name,\n" +
                "teacher_info,\n" +
                "preview_first_field,\n" +
                "preview_second_field,\n" +
                "discounts,\n" +
                "price,\n" +
                "price_tag,\n" +
                "course_img_url,\n" +
                "share_image_title,\n" +
                "share_title,\n" +
                "share_description,\n" +
                "course_description,\n" +
                "STATUS\n" +
                "FROM course WHERE id = ?;";

        //query

            List<Course> list = qr.query(sql, new BeanListHandler<Course>(Course.class), id);
            return list;
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return null;
    }
}
